<?php
require_once 'connexion (2).php';
$trie=null;
if(!(isset($_GET['trie'])))
{
    $trie='';
}else{ $trie=isset($_GET['trie']); echo $trie;}


///// vérification des cases à cocher de formation
if(isset($_POST['filtre'])){

$req_ran="select * from formation";
$resu_ra=$connexion->query($req_ran);


$nbres=$resu_ra->rowCount();
$sql="SELECT * from entreprise,correspond WHERE correspond.IDENTREPRISE = entreprise.IDENTREPRISE AND IDFORMATION IN (0,";
for($i=0;$i<$nbres/2;$i++)
   {
       for($j=0;$j<3;$j++)
            {

           if ($rep=$resu_ra->fetch(PDO::FETCH_ASSOC))
                          {
                            if(isset($_POST[$rep["IDFORMATION"]]))
                                 $sql.="".$rep["IDFORMATION"] .",";  
                            
                          }
            }
   }
 $sql= substr($sql,0,-1);
$sql.=")";  
   
}else{
    $sql="SELECT * from entreprise,correspond WHERE correspond.IDENTREPRISE = entreprise.IDENTREPRISE "; 
}

/*
$st = $connexion->query($sql);
$nbEnt=$st->rowCount();*/
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <link rel="stylesheet" href="feuilledestyle.css">
        <link rel="stylesheet" href="menuHead/jquery.cleditor.css">
        <link href="menu2/styles.css" rel="stylesheet" type="text/css">
        <script type="text/javascript">
            function ctrlCheck()
            {
            var inputs = document.getElementsByTagName('input');
           
           
                    var c = inputs.length;
                    
                    for(var i = 1; i < c; i++) 
                        {

                            var attb=inputs[i].getAttribute('type');
                            if(attb=='checkbox')
                                {
                                    if(inputs[i].checked==true)
                                            { 

                                                for(var e = i+1; e < c; e++) 
                                                    {
                                                         inputs[e].checked=true;
                                                    }
                                            }else
                                                 {
                                                     for(var e = i+1; e < c; e++) 
                                                        {
                                                             inputs[e].checked=false;
                                                        }
                                                 }
                                            
                                }

                        }
            }
        
         function test()
            {
                var inputs = document.getElementsByTagName('input');
           
           
                    var c = inputs.length;
                     var t=0;
                    for(var i = 1; i < c; i++) 
                        {

                            var attb=inputs[i].getAttribute('type');
                            if(attb=='checkbox')
                                {
                                   // alert(attb.length);
                                  // var e = i+1;
                                      /**/ for(var e = i+1; e < c; e++) 
                                        {
                                           
                                            if(inputs[e].checked==true)
                                                { 
                                                    t=t+1;
                                                }
                                        }          
                                         
                                          inputs[0].checked=true;
                                          
                                          if(t==e-2)
                                              {
                                                   inputs[0].checked=true;
                                                   
                                              }else{
                                                   inputs[0].checked=false;
                                                   
                                              }
                                }
                           
                        }
                // alert(e-2+' le nb de checkbox '+t);
                 
                 
                 
            }
        </script>
    </head>
    <body vlink="blue">
        
        
     
        <div id="page">
        <?php include 'header.php';?>
        <div id="cadre">
        <?php include 'menu.php';?> 
            <h5 style="float:left; margin-top: 20px; background-color: orange; "> <INPUT TYPE="button" VALUE="Retour" onClick="history.back()"> </h5> 
                <div class="centre">
                    
                <script>
                function myFunction()
                {
                var x=document.getElementById("demo");
                var Whit = document.getElementById("fgdfes");
                d=Whit.getAttribute('name');
                alert(d);
                x.innerHTML=Whit.childNodes.length;
                }
                </script>
                     <h3  style="margin-bottom: 50px; color: darkslateblue;">Liste des Entreprises </h3>
                            
                    <table border="10" align="center" style="background-color:palegoldenrod;">
                        
                         <form name="detailform" id="detailform" method="POST">
                              <input type="hidden"  name="filtre" />

                            <?php 
                                // ib regarde si on avait coché "tout"
                                if(isset($_POST["tout"]))          
                                    echo '</tr><td style="float:left;">
                                          <input type="checkbox" id="0" name="tout" onclick="ctrlCheck()" checked>Tous</td></tr>';
                                else
                                    echo '</tr><td style="float:left;">
                                          <input type="checkbox" id="0" name="tout" onclick="ctrlCheck()">Tous</td></tr>';
                                
                                
                                      $req_ran="select * from formation";
                                      $resu_ra=$connexion->query($req_ran);

                                      $nbres=$resu_ra->rowCount();

                                            echo '<tr>';
                                                 while ($rep=$resu_ra->fetch(PDO::FETCH_ASSOC))
                                                                {echo '<td style="float:left; color:darkslateblue; font-size:70%;">';
                                                                   $id=$rep["IDFORMATION"];
                                                                   $desc=$rep["LIBELLEFORMATION"]; 
                                                                   //on vérifie si on a déjà coché ces cases
                                                                   if(isset($_POST[$rep["IDFORMATION"]]))
                                                                        echo '<input type="checkbox" id="'.$id.'" name="'.$id.'" onclick="test()" checked><b>'.$desc.'</b></td>';
                                                                   else
                                                                        echo '<input type="checkbox" id="'.$id.'" name="'.$id.'" onclick="test()"><b>'.$desc.'</b></td>';
                                                                echo '</td>';  }
                                                     
                                                 echo" <tr><br>";

                             ?>
                              <tr><td><input type="submit"/></td></tr>
                         </form>
                         
                    </table><br>
                            
                            <table border="12" align="center" style="background-color:#CvfCC; color: slatefgray; max-width:600px;">
                                
                                <tr>    
                                    <th style="min-width:320px;">Nom entreprise :  <?php  //echo "(".$nbEnt.")";?></th>
                                </tr>
                                <?php 
                                               
                                              $req_rando="select * from entreprise";
                                              
                                              
                                              $resu_ra=$connexion->query($sql);
                                              
                                                   while ($rep=$resu_ra->fetch(PDO::FETCH_ASSOC))
                                                   {
                                                       $id=$rep["IDENTREPRISE"];
                                                       $desc=$rep["NOMENTREPRISE"]; 
                                                       echo '<tr>
                                                                 <td style="float:leftd; font-size:80%;"><a href="infoEntreprise.php?id='.$id.'" target="_block">'.$desc.'</a> </td>
                                                             </tr>';
                                                      
                                                   }
                                                  
                                              ?>
                               
                                
                               
                            </table>
                       
                        
                </div>
                 
                 
        <?php require_once 'pied.php';?> 
        </div> 
        </div>
    </body>
</html>
 